/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.card.service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.file.FileNameUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.card.mapper.BusinessCardMapper;
import top.continew.admin.card.model.entity.BusinessCardDO;
import top.continew.admin.card.model.query.BusinessCardQuery;
import top.continew.admin.card.model.req.BusinessCardReq;
import top.continew.admin.card.model.resp.BusinessCardDetailResp;
import top.continew.admin.card.model.resp.BusinessCardResp;
import top.continew.admin.card.service.BusinessCardService;
import top.continew.admin.card.service.CardIntroductionRelationService;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.io.IOException;

/**
 * 微信名片业务实现
 *
 * @author floyd
 * @since 2024/11/22 15:10
 */
@Service
@RequiredArgsConstructor
public class BusinessCardServiceImpl extends BaseServiceImpl<BusinessCardMapper, BusinessCardDO, BusinessCardResp, BusinessCardDetailResp, BusinessCardQuery, BusinessCardReq> implements BusinessCardService {

    private final String[] avatarSupportSuffix = {};
    private final CardIntroductionRelationService relationService;

    @Override
    public String getAvatar(MultipartFile avatarFile) throws IOException {
        String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
        //        CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String
        //                .join(StringConstants.CHINESE_COMMA, avatarSupportSuffix));

        return ImgUtil.toBase64DataUri(ImgUtil.scale(ImgUtil.toImage(avatarFile
            .getBytes()), 100, 100, null), avatarImageType);
    }

    @Override
    public void saveIntroduces(BusinessCardReq req, long cardId) {
        relationService.add(req.getCompanyIntroduces(), cardId);
    }
}